package com.vma.jifu.service.impl;

import com.jfinal.plugin.activerecord.Page;
import com.vma.jifu.bizModel.model.AliAccount;
import com.vma.jifu.bizModel.model.BankAccount;
import com.vma.jifu.bizModel.model.CashOut;
import com.vma.jifu.bizModel.model.WxAccount;
import com.vma.jifu.service.AccountService;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by chenzui on 2017/7/1.
 */
public class AccountServiceImpl implements AccountService {
    public void addAccount(Integer userId,String aliName, String aliAccount, String wxName, String wxAccount) {
        AliAccount aliAccountObj = AliAccount.dao.findFirst("SELECT * FROM ali_account WHERE user_id = ?",userId);
        if(aliAccountObj == null){
            new AliAccount().setAccountName(aliName).setAliAccount(aliAccount).setUserId(userId).setCreateTime(new Date()).save();
        }else {
            aliAccountObj.setAccountName(aliName).setAliAccount(aliAccount).update();
        }

        WxAccount wxAccount1 = WxAccount.dao.findFirst("select * from wx_account where user_id = ?",userId);
        if(wxAccount1 == null){
            new WxAccount().setUserId(userId).setAccountName(wxName).setCreateTime(new Date()).setWxAccount(wxAccount).save();
        }else {
            wxAccount1.setAccountName(wxName).setWxAccount(wxAccount).update();
        }


    }

    public Map getMyAccount(Integer userId) {
        AliAccount aliAccountObj = AliAccount.dao.findFirst("SELECT * FROM ali_account WHERE user_id = ?",userId);
//        BankAccount bankAccount = BankAccount.dao.findFirst("select * from bank_account where user_id = ?",userId);
        WxAccount wxAccount = WxAccount.dao.findFirst("select * from wx_account where user_id = ?",userId);

        Map result = new HashMap();
        result.put("aliAccount",aliAccountObj);
//        result.put("bank",bankAccount);
        result.put("wxAccount",wxAccount);
        return result;
    }

    public void checkOutMoney(Integer userId, Integer type, Integer number, Integer accountId) {
//        1已打款0未处理-1打款失败2处理中3拒绝
        new CashOut().setUserId(userId).setType(type).setCashNum(number).setCreateTime(new Date())
                .setAccountId(accountId).setStatus(0).save();
    }

    public Page<CashOut> qryCashOutList(Integer userId, Integer cPage, Integer pSize) {
        Page<CashOut> cashOutPage = CashOut.dao.paginate(cPage,pSize,
                "SELECT *",
                "FROM cash_out WHERE user_id = ? ",userId);

        return cashOutPage;
    }
}
